package cl.regalloc.parse;

import java.util.HashMap;

public class StackLocationFactory {

	private static HashMap<String, StackLocation> stackLocationSet = new HashMap<String, StackLocation>();

	public StackLocationFactory() {
		super();
	}

	public static StackLocation getStackLocation(String stackLoctStr) {

		if (stackLocationSet.containsKey(stackLoctStr)) {
			System.out.printf("stack location \"%s\" already exists, returning reference%n", stackLoctStr);
			return stackLocationSet.get(stackLoctStr);
		} else {
			String offset = stackLoctStr.substring(0, stackLoctStr.indexOf("("));
			offset = offset.substring(2);	// omit the 0x prefix
			System.out.printf("stack location offset \"%s\" extracted%n", offset);
			String base = stackLoctStr.substring(stackLoctStr.indexOf("(") + 1, stackLoctStr.indexOf(")"));
			System.out.printf("stack location base \"%s\" extracted%n", base);

			StackLocation currentStackLocation = new StackLocation(Integer.parseInt(offset, 16), RegisterFactory.getRegister(base));
			stackLocationSet.put(stackLoctStr, currentStackLocation);
			return currentStackLocation;
		}
	}
}
